var passwordInput = document.getElementById("password_input");
// 监听输入框的keypress事件
passwordInput.addEventListener("keypress", function (event) {
    // 判断按下的键是否是Enter键（keyCode为13）
    if (event.key === "Enter") {
        // 调用login()函数
        login();
    }
});

async function login() {
    username_input = document.querySelector("#username_input");
    password_input = document.querySelector("#password_input");
    if (username_input.value === "" || password_input.value === "") {
        showPopup("用户名或密码不能为空");
        return;
    }
    //登入
    api = baseUrl + "/login";
    data = {
        username: username_input.value,
        password: password_input.value
    };
    showPopup("登入中...请稍等...");
    await fetch(api, {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
    }).then(response => response.json())
        .then(data => {
            console.log(data);
            if (data["code"] == 404) {
                showPopup("找不到目标，404");
                return;
            }
            if (data["msg"] != "执行成功") {
                username_input.value = "";
                password_input.value = "";
                showPopup(data["msg"]);
            } else {
                setCookie("uid", data["data"]["id"]);
                setCookie("grade", data["data"]["grade"]);
                setCookie("token", data["token"]);
                window.location.href = "./html/exercise.html";
            }
        }).catch(error => {
            console.error("出现错误：" + error);
            username_input.value = "";
            password_input.value = "";
            showPopup("服务器异常...");
            return;
        })
}
function forget() {
    showPopup("还没设计该功能");
}